/**
 * @author zhanglongsheng
 * @since 2018.12.13
 */

function perOrder(param, ordNo) {
    if (typeof WeixinJSBridge == "undefined") {
        if (document.addEventListener) {
            document.addEventListener("WeixinJSBridgeReady", onBridgeReady, false);
        }else if (document.attachEvent) {
            document.attachEvent("WeixinJSBridgeReady", onBridgeReady);
            document.attachEvent("onWeixinJSBridgeReady", onBridgeReady);
        }
    }else {
        console.log("微信支付入参param", param);
        console.log("微信支付入参ordNo", ordNo);
        onBridgeReady(param, ordNo);
    }
    
    function onBridgeReady(param, ordNo) {
        var jsapi = eval('(' + param + ')');
        WeixinJSBridge.invoke('getBrandWCPayRequest', jsapi, function (res) {
           var result = '';
           var errMsg = "";
           if (res.err_msg == 'get_brand_wcpay_request:ok') {
               result = 'success';
           }else {
               result = 'error';
               errMsg = "支付失败";
           }
            console.info("当前请求响应:",res)
            location.href = root + "pay/notice?ordNo=" + ordNo + "&result=" + result + "&errMsg=" + errMsg;
        });
    }
}

function close() {
    if (typeof WeixinJSBridge == 'undefined') {
        if (document.addEventListener) {
            document.addEventListener('WeixinJSBridgeReady', closeWindow, false);
        }else if (document.attachEvent) {
            document.attachEvent('WeixinJSBridgeReady', closeWindow);
            document.attachEvent('onWeixinJSBridgeReady', closeWindow);
        }
    }else {
        closeWindow();
    }
    function closeWindow() {
        WeixinJSBridge.call('hideOptionMenu');
    }
}

$(function () {
   function onBridgeReady() {
       WinxinJSBridge.call('hideOptionMenu');
   }
   if (typeof WeixinJSBridge == 'undefined') {
       if (document.addEventListener) {
           document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
       }else if (document.attachEvent) {
           document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
           document.attachEvent('onWeixinJSBridgeReady', onBridgeReady)
       }
   }else {
       onBridgeReady();
   }
});